/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package InternetHax;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * Gets a direction from the player
 * @author LoginError
 */
public class DirectionMenu extends Menu {
    Image fourWay;
    Image eightWay;
    boolean allowDiagonals = false;
    boolean allowCenter = false;
    int returnValue = -1;
    public DirectionMenu(Image fourWay, Image eightWay) {
        this.fourWay = fourWay;
        this.eightWay = eightWay;
    }
    
    /**
     * Returns a direction entered by the user as specified in {@link Constants}.
     * Returns -1 if the user cancelled by pressing 0 or in the event of an error;
     * @param allowDiagonals if true allow diagonal directions to be returned
     * @return
     */
    public synchronized int getDirection(boolean allowDiagonals, boolean allowCenter)
    {
        reset();
        this.allowDiagonals = allowDiagonals;
        this.allowCenter = allowCenter;
        Gamestate.modalMenu = this;
        try {
            this.wait();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        Gamestate.modalMenu = null;
        return (Gamestate.invertNumpad ? Toolbox.flipVertical(returnValue) : returnValue); //if we're using a computer numpad, flip the direction vertically
    }
    
    public synchronized void paint(Graphics g, int x, int y) {
        g.drawImage((allowDiagonals == true ? eightWay : fourWay),x,y,Graphics.HCENTER | Graphics.VCENTER);
    }

    private void reset()
    {
        returnValue = -1;
    }

    public synchronized void handleInput(int keyAction) {
        
        switch(keyAction)
        {
            case HaxCanvas.UP: 
            case HaxCanvas.KEY_NUM2:
                returnValue = Constants.DIRECTION_UP;
            break;
            
            case HaxCanvas.DOWN:
            case HaxCanvas.KEY_NUM8:
                returnValue = Constants.DIRECTION_DOWN;
            break;
            
            case HaxCanvas.LEFT:
            case HaxCanvas.KEY_NUM4:
                returnValue = Constants.DIRECTION_LEFT;
            break;
            
            case HaxCanvas.RIGHT:
            case HaxCanvas.KEY_NUM6:
                returnValue = Constants.DIRECTION_RIGHT;
            break;
        
            case HaxCanvas.KEY_NUM1:
                if(allowDiagonals) returnValue = Constants.DIRECTION_UP_LEFT;
            break;
            
            case HaxCanvas.KEY_NUM3:
                if(allowDiagonals) returnValue = Constants.DIRECTION_UP_RIGHT;
            break;
            
            case HaxCanvas.KEY_NUM7:
                if(allowDiagonals) returnValue = Constants.DIRECTION_DOWN_LEFT;
            break;
        
            case HaxCanvas.KEY_NUM9:
                if(allowDiagonals) returnValue = Constants.DIRECTION_DOWN_RIGHT;
            break;
        
            case HaxCanvas.KEY_NUM5: 
                if(allowCenter) returnValue = Constants.DIRECTION_CENTER;
            break;
            
            case HaxCanvas.KEY_NUM0:{ //cancel
                this.notify();
                return;
            }
            
        }
       
        if(returnValue != -1)
            this.notify();
    }
}
